//logs.js
const util = require('../../utils/util.js')
var app = getApp()
var url = app.globalData.url
var url1 = app.globalData.url1
var orderId = ""
var tel=''
var billEmail = ""
var billTt = ""
var billSh = 0
var billType = ""
var price = ''
var orderType = ''
Page({
  data: {
    steps: [{
        desc: '待接单',
        progress: "10",
        img:"15",
        percent: 20
      }, {
        desc: '在路上',
        progress: "40",
        percent: 40,
        img: "162",
      }, {
        desc: '已上门',
        progress: "70",
        percent: 60,
        img: "325",
      },
      {
        desc: '待付款',
        progress: "100",
        percent: 80,
        img: "480",
      },
      {
        desc: '待评价',
        progress: "130",
        percent: 100,
        img: "633",
      }
    ],
    current: 4,
    details: {},
    flag: true,
    names: "订单详情",
    cur: 0,
    color1: "#fff",
    color2: "#fff",
  },
  onLoad: function(options) {
   this.getOrderDetail(options.mid)
    this.setData({
      cur:options.cur
    })
  },

  getOrderDetail(mid) {
    const that = this
    wx.request({
      url: url1+'/orders/findOne',
      data: {
        orderId:mid
      },
      header: {
        'content-type': "application/json"
      },
      method: "get",
      success(res) {
        console.log('订单数据')
        console.log(res.data)
        if (res.data.code == 1) {
          var details = res.data.data.orderDTO
          orderId=details.orderId
          if (details.orderStatus == -2) {
            that.setData({
              current: 0
            })
          } else
          if (details.orderStatus == 0) {
            that.setData({
              current: 1
            })
          } else
          if (details.orderStatus == 5) {
            that.setData({
              current: 2
            })
          } else
          if (details.orderStatus == 1) {
            that.setData({
              current: 3
            })
          } else
          if (details.orderStatus == 2) {
            that.setData({
              current: 4
            })
          }
          that.setData({
            details: details,
          })
        }
      }
    })
  },
  toEngineer(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })
  },
  toKefu(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
    })
  },
  gotoAccess(){
    wx.navigateTo({
      url: '../access/access?orderid=' + orderId,
    })
  },
  goPay(){
    wx.navigateTo({
      url: '../topay/topay?orderId='+orderId,
    })
  },

  kaipiao(e) {
    const that = this
    console.log(e)
    that.showPopup()
    orderType = e.currentTarget.dataset.type
    that.setData({
      orderId: orderId
    })
  },

  selectBilltype1() {

    console.log("单位")
    const that = this
    billType = "企业发票"
    that.setData({
      color1: "#A01DE3",
      color2: "#fff"
    })
  },
  selectBilltype2() {
    console.log("个人发票")
    billType = "个人发票"
    const that = this
    that.setData({
      color2: "#A01DE3",
      color1: "#fff"
    })
  },
  // 发票抬头
  getbillTt(e) {
    billTt = e.detail.value
  },
  // 发票税号
  getbillsh(e) {
    billSh = e.detail.value
  },
  // 邮箱
  getbillemail(e) {
    console.log(e)
    billEmail = e.detail.value
  },
  addbills() {
    const that = this
    var re = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;

    if (billEmail.length == 0 ||
      billTt.length == 0 || billType.length == 0) {
      wx.showToast({
        title: '请输入完整信息',
        icon: "loading",
        duration: 1000
      })
    } else if (!re.test(billEmail)) {
      wx.showToast({
        title: '邮箱不正确',
        icon: "loading",
        duration: 1000
      })
    } else if (billType == "单位" && billSh.length == 0) {
      wx.showToast({
        title: '请输入企业税号',
        icon: "loading",
        duration: 1000
      })
    } else {
      // 开票
      wx.request({
        url: url+'/Invoice/KaiPIao',
        data: {
          orderId: orderId,
          invoiceType: billType,
          iHead: billTt,
          shuihao: billSh,
          youxiang: billEmail,
          iPrice: price,
          orderMenu: orderType
        },
        header: {
          'content-type': "application/json"
        },
        method: "get",
        success(res) {
          console.log('订单开票数据')
          console.log(res.data)
          if (res.data.code == 1) {
            wx.showToast({
              title: '开票成功请注意邮箱',
              icon: "loading",
              duration: 1000
            })
            that.popupTap()
            that.getOrders(cur, 0)

          } else {
            wx.showToast({
              title: '开票失败',
              icon: "loading",
              duration: 1000
            })
          }
        }
      })

    }
  },
  popupTap() {
    let popupComponent = this.selectComponent('.J_Popup');

    popupComponent && popupComponent.toggle(false);
  },

  showPopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    /*popupComponent && popupComponent.show();*/
    popupComponent && popupComponent.toggle(true);
  },
  hidePopup() {
    let popupComponent = this.selectComponent('.J_Popup');
    //popupComponent && popupComponent.hide();
    popupComponent && popupComponent.toggle();
  },

  back() {
    wx.navigateBack(1)
  }
})